﻿using B.S.DataBase.MyProject.Domain.Management;
using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.Management;
using MedicalPCTerminal.Read.API.Dtos;
using SqlSugar;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.Management
{
    /// <summary>
    /// 处理健康报告显示命令
    /// </summary>
    public class GetReportsCommandHandler : IRequestHandler<GetReportsCommand, ApiPaging<NewReportsDto>>
    {
        /// <summary>
        /// sqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="sqlSugar">sqlSugar</param>
        public GetReportsCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiPaging<NewReportsDto>> Handle(GetReportsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                ApiPaging<NewReportsDto> result = new ApiPaging<NewReportsDto>();
                var lists = sqlSugar.db.Queryable<Reports, User>((a, b) => new object[]
                {
                    JoinType.Left,a.UserId==b.UserId
                }).Select<NewReportsDto>((a, b) => new NewReportsDto
                {
                    CreateTime = a.CreateTime,
                    EtartDate = a.EtartDate,
                    StartDate = a.StartDate,
                    UserName = b.UserName,
                    UserId = a.UserId,
                    Status = a.Status,
                    Publisher = a.Publisher,
                    quarter = a.quarter,
                    Reportingperiod = a.Reportingperiod,
                    ReportsId = a.ReportsId,
                    ReportsType = a.ReportsType,
                }).ToList();
                if (!string.IsNullOrEmpty(request.ReportsType))
                {
                    lists = lists.Where(x => x.ReportsType == request.ReportsType).ToList();
                }
                if (request.CreateTime != null)
                {
                    lists = lists.Where(x => x.StartDate <= request.CreateTime && x.EtartDate >= request.CreateTime).ToList();
                }
                result.TotalCount = lists.Count();
                result.TotalPage = (int)Math.Ceiling(lists.Count() * 1.0 / request.PageSize);
                lists = lists.OrderByDescending(x => x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.list = lists.ToList();
                return await Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
